$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'mercharReport/storematerialout/list',
        datatype: "json",
        colModel: [			
			{ label: '单据号', name: 'outNo', index: 'outNo', width: 50 },
			{ label: '含税总金额', name: 'totalMoney', index: 'totalMoney', width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'createTime', width: 80 },
			{ label: '商品id', name: 'materialId', index: 'materialId', width: 80 },
			{ label: '商品名称', name: 'materialName', index: 'materialName', width: 80 },
			{ label: '商品数量', name: 'materialQuantity', index: 'materialQuantity', width: 80 },
			{ label: '单位', name: 'unit', index: 'unit', width: 80 },
			{ label: '仓库ID', name: 'stockId', index: 'stockId', width: 50 },
			{ label: '仓库', name: 'stockName', index: 'stockName', width: 80 },
			{ label: '库存交易类型', name: 'type', index: 'type', width: 80 },
			{ label: '克重', name: 'weight', index: 'weight', width: 80 },
			{ label: '长(单位米)', name: 'length', index: 'length', width: 80 },
			{ label: '宽(单位米)', name: 'width', index: 'width', width: 80 },
			{ label: '备注', name: 'remark', index: 'remark', width: 80 }
        ],
		viewrecords: true,
        height: window.innerHeight-125,
        rowNum: 25,
        rowList: [25, 50, 80],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.storeOutEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').storeOutEnum;
            vm.totalMoney = $("#jqGrid").jqGrid('getGridParam', 'userData').totalMoney;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		storeMaterialOut: {},
		q:{
            outNo:'',
            materialId:'',
            materialName:'',
            type:'',
            beginCreatetime:'',
            endCreatetime:''
		},
        storeOutEnum:[],
        totalMoney:''
	},
	methods: {
        checkBeginDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
        checkEndDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.storeMaterialOut = {};
		},
		update: function (event) {
			var outId = getSelectedRow();
			if(outId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(outId)
		},
		saveOrUpdate: function (event) {
			var url = vm.storeMaterialOut.outId == null ? "mercharReport/storematerialout/save" : "mercharReport/storematerialout/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.storeMaterialOut),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var outIds = getSelectedRows();
			if(outIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "mercharReport/storematerialout/delete",
                    contentType: "application/json",
				    data: JSON.stringify(outIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(outId){
			$.get(baseURL + "mercharReport/storematerialout/info/"+outId, function(r){
                vm.storeMaterialOut = r.storeMaterialOut;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'outNo': vm.q.outNo.trim(),
                    'materialId': vm.q.materialId.trim(),
                    'materialName': vm.q.materialName.trim(),
                    'type': vm.q.type,
                    'beginCreatetime': vm.q.beginCreatetime,
                    'endCreatetime': vm.q.endCreatetime,
                },
                page:1
            }).trigger("reloadGrid");
		},
        reset() {
            vm.q.outNo = '';
            vm.q.materialId = '';
            vm.q.materialName = '';
            vm.q.type = '';
            vm.q.beginCreatetime = '';
            vm.q.endCreatetime = '';
            vm.reload();
        },
        exportExcel() {
            //导出excel
            vm.q.outNo = vm.q.outNo.trim();
            vm.q.materialId = vm.q.materialId.trim();
            vm.q.materialName = vm.q.materialName.trim();
            const data = encodeURIComponent(JSON.stringify(vm.q));
            window.location.href = baseURL + "mercharReport/storematerialout/exportExcel?postData=" + data;
        }
	}
});